package com.aeoj.clientmanagement.enums.problem;

import lombok.Getter;

/**
 * 题目提交编程语言枚举
 */
@Getter
public enum ProblemSubmitLanguageEnum {
    JAVA("Java", "java"),
    CPP("C++", "cpp"),
    PHP("Php", "php"),
    JAVASCRIPT("JavaScript", "JavaScript");

    private final String name;      // 语言名称（显示用）
    private final String code;      // 语言代码（标识用）

    ProblemSubmitLanguageEnum(String name, String code) {
        this.name = name;
        this.code = code;
    }

    /**
     * 根据语言代码获取枚举
     *
     * @param code 语言代码
     * @return 对应的枚举，找不到返回null
     */
    public static ProblemSubmitLanguageEnum getByCode(String code) {
        if (code == null) {
            return null;
        }
        for (ProblemSubmitLanguageEnum language : ProblemSubmitLanguageEnum.values()) {
            if (language.getCode().equalsIgnoreCase(code)) {
                return language;
            }
        }
        return null;
    }

    /**
     * 检查语言代码是否有效
     *
     * @param code 语言代码
     * @return 是否有效
     */
    public static boolean isValidCode(String code) {
        return getByCode(code) != null;
    }

    /**
     * 根据语言名称获取枚举
     *
     * @param name 语言名称
     * @return 对应的枚举，找不到返回null
     */
    public static ProblemSubmitLanguageEnum getByName(String name) {
        if (name == null) {
            return null;
        }
        for (ProblemSubmitLanguageEnum language : ProblemSubmitLanguageEnum.values()) {
            if (language.getName().equalsIgnoreCase(name)) {
                return language;
            }
        }
        return null;
    }
}